import { Ctrl } from "./typeing";


interface ControllerType{
    __init: ()=> ControllerType;
    __reactive: ()=> void;
    // data: {},
}


class Controller implements ControllerType {
  private __init = function (initParams: Ctrl) {
    this.$changer = initParams.$changer;
    this.data = this.__reactive();
    return this;
  };
  private __reactive = function (data) {
    var _this = this;
    var handler = {
      get(target, key) {
        //懒监听，去获取的时候才监听对象里面的对象，而不是直接递归循环监听
        if (typeof target[key] === 'object' && target[key] !== null) {
          return new Proxy(target[key], handler);
        }
        return Reflect.get(target, key);
      },
      set(target, key, value) {
        console.log('set', target, key, value);
        //数组新增会执行两次，一次是修改length，一次是添加值
        let oldValue = target[key];
        if (!oldValue) {
          //找不到老值，新增
        } else if (oldValue !== value) {
          //老值和新值不相等，修改
        }
        _this.$changer();
        return Reflect.set(target, key, value);
      },
    };
    return new Proxy(this.data, handler);
  };
}

export default Controller;
